package com.lecture.javamode.principle.ocp.before;

/**
 * @author luyn
 * @version 1.0
 * @description 通知实现类
 * @date 2022/1/11 13:44
 */
public class Alter {

    private AlterRule rule;
    private Notification notification;

    public Alter(AlterRule alterRule, Notification notification) {
        this.rule = alterRule;
        this.notification = notification;
    }

    //检验接口是否超过设置最大值
   /* public void check(String api, long requestCount, long errorCount, long durationOfSeconds) {
        long tps = requestCount / durationOfSeconds;
        if (tps > rule.getMatchedRule(api).getMaxTps()) {
            //如果接口实际的tps值超过了设计的最大tps值，就发送报警信息
            notification.notify(NotificationEmergencyLevel.URGENCY, "告警");
        }
        if (errorCount > rule.getMatchedRule(api).getMaxErrorCount()) {
            notification.notify(NotificationEmergencyLevel.SEVERE, "告警");
        }
    }*/

    //如果需要增加一个功能，当每秒钟接口请求个数超过设置的最大值，就发送告警通知，此时我们需要修改check方法
    //增加timeoutCount参数，增加验证规则
    public void check(String api, long requestCount, long errorCount, long durationOfSeconds,long timeoutCount) {
        long tps = requestCount / durationOfSeconds;
        if (tps > rule.getMatchedRule(api).getMaxTps()) {
            //如果接口实际的tps值超过了设计的最大tps值，就发送报警信息
            notification.notify(NotificationEmergencyLevel.URGENCY, "告警");
        }
        if (errorCount > rule.getMatchedRule(api).getMaxErrorCount()) {
            notification.notify(NotificationEmergencyLevel.SEVERE, "告警");
        }

        // 改动二：添加接口超时处理逻辑
        long timeoutTps = timeoutCount / durationOfSeconds;
        if (timeoutTps > rule.getMatchedRule(api).getMaxTimeoutTps()) {
            notification.notify(NotificationEmergencyLevel.URGENCY, "...");
        }
        //这种方式每增加一种检验规则就需要修改当前代码，不符合开闭原则
    }

}
